layui.define(['jquery'], function(exports) {
	"use strict";

	var MOD_NAME = 'pageButton';
	var $ = layui.jquery;


    var pageButton = {
        config: {
            elem: null,
            data: {}
        },
        init: function() {
            let that = this;
            if(this.config.url) {
                $.ajax({
                    url: this.config.url,
                    async: false,
                    method: 'get',
                    success: function(result) {
                        that.config.data = result;
                    }
                });
            }

        },
        render: function(options) {
            this.config = $.extend(this.config, options);
            this.init();
            let list = this.config.data;
            if(list && list.length > 0){
                for(let j = 0; j < list.length; j++) {
                    let item = list[j];
                    if (item.type === 'BTN_PAGE') {
                        // 调用创建按钮的函数
                        this.createButton(item, ['pear-btn', 'pear-btn-md', 'pear-btn-primary'], this.config.elem);
                    }
                }
            }
        },
        createButton: function (item, buttonClass, container){
            // 创建按钮元素
            const button = document.createElement('button');
            button.classList.add(...buttonClass);  // 设置按钮的 class
            button.setAttribute('lay-event', item.btnEvent);  // 设置 lay-event 属性
            button.style.marginRight = '5px';  // 设置行内样式

            // 创建按钮文本节点
            const textNode = document.createTextNode(item.title);  // 设置按钮文本
            // 将文本添加到按钮中
            button.appendChild(textNode);

            // 将按钮添加到指定容器中
            $(container).append(button);
        },
        btnTableRender: function(options) {
            let tableButton='';
            this.config = $.extend(this.config, options);
            this.init();
            let list = this.config.data;
            if(list && list.length > 0){
                for(let j = 0; j < list.length; j++) {
                    let item = list[j];
                    if (item.type === 'BTN_TABLE') {
                        // 加入默认值检查
                        let btnClass = item.btnClass || '';
                        let btnEvent = item.btnEvent || '';
                        let title = item.title || '';
                        let icon = item.icon || '';
                        // 使用模板字符串来生成 HTML
                        tableButton += `
                            <a href="javascript:void(0);" lay-event="${btnEvent}" style="color:#003866;margin-left: 5px;">${title}</a>
                          
                        `;
                    }
                }
            }
            return tableButton;
        }
    };
	exports(MOD_NAME, pageButton);
})